package com.ruoyi.web.controller.community;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.HeartElderlyInfo;
import com.ruoyi.community.service.IHeartElderlyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 老人信息Controller
 * 
 * @author ruoyi
 * @date 2021-07-26
 */
@RestController
@RequestMapping("/community/elderly")
public class HeartElderlyInfoController extends BaseController
{
    @Autowired
    private IHeartElderlyInfoService heartElderlyInfoService;

    /**
     * 查询老人信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HeartElderlyInfo heartElderlyInfo)
    {
        startPage();
        List<HeartElderlyInfo> list = heartElderlyInfoService.selectHeartElderlyInfoList(heartElderlyInfo);
        return getDataTable(list);
    }

    @GetMapping("/byUserId")
    public TableDataInfo byUserId()
    {
        startPage();
        Long userid = SecurityUtils.getUserID();
        HeartElderlyInfo heartElderlyInfo = new HeartElderlyInfo();
        heartElderlyInfo.setUserId(userid);
        List<HeartElderlyInfo> list = heartElderlyInfoService.selectHeartElderlyInfoList(heartElderlyInfo);
        return getDataTable(list);
    }
    /**
     * 返回老人图表信息
     */
    @GetMapping("/elderlyChart")
    public Map<String,Integer> elderlyChart()
    {
        Map<String,Integer> map = new HashMap<>();
        Integer count60and65 = heartElderlyInfoService.countElderlyAge60and65();
        map.put("count60and65",count60and65);
        Integer count66and70 = heartElderlyInfoService.countElderlyAge66and70();
        map.put("count66and70",count66and70);
        Integer count71and75 = heartElderlyInfoService.countElderlyAge71and75();
        map.put("count71and75",count71and75);
        Integer count76and80 = heartElderlyInfoService.countElderlyAge76and80();
        map.put("count76and80",count76and80);
        Integer count81and85 = heartElderlyInfoService.countElderlyAge81and85();
        map.put("count81and85",count81and85);
        Integer countUp86 = heartElderlyInfoService.countElderlyAgeUp86();
        map.put("countUp86",countUp86);
        Integer countWoman = heartElderlyInfoService.countElderlyWoman();
        map.put("countWoman",countWoman);
        Integer countMan = heartElderlyInfoService.countElderlyMan();
        map.put("countMan",countMan);
        return map;
    }

    /**
     * 导出老人信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:elderly:export')")
    @Log(title = "老人信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HeartElderlyInfo heartElderlyInfo)
    {
        List<HeartElderlyInfo> list = heartElderlyInfoService.selectHeartElderlyInfoList(heartElderlyInfo);
        ExcelUtil<HeartElderlyInfo> util = new ExcelUtil<HeartElderlyInfo>(HeartElderlyInfo.class);
        return util.exportExcel(list, "老人信息数据");
    }

    /**
     * 获取老人信息详细信息
     */
    @GetMapping(value = "/{elderlyid}")
    public AjaxResult getInfo(@PathVariable("elderlyid") Long elderlyid)
    {
        return AjaxResult.success(heartElderlyInfoService.selectHeartElderlyInfoById(elderlyid));
    }

    /**
     * 新增老人信息
     */
    @Log(title = "老人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeartElderlyInfo heartElderlyInfo)
    {

        Long userid = SecurityUtils.getUserID();
        HeartElderlyInfo temp = new HeartElderlyInfo();
        temp.setUserId(userid);
        List<HeartElderlyInfo> list = heartElderlyInfoService.selectHeartElderlyInfoList(temp);
        if(list.size() > 0){
            return AjaxResult.error(300, "您已认证过");
        }
        heartElderlyInfo.setCommunityid(SecurityUtils.getCommunityId().longValue());
        return toAjax(heartElderlyInfoService.insertHeartElderlyInfo(heartElderlyInfo));
    }

    /**
     * 修改老人信息
     */
    @Log(title = "老人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HeartElderlyInfo heartElderlyInfo)
    {
        return toAjax(heartElderlyInfoService.updateHeartElderlyInfo(heartElderlyInfo));
    }

    /**
     * 删除老人信息
     */
    @Log(title = "老人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{elderlyids}")
    public AjaxResult remove(@PathVariable Long[] elderlyids)
    {
        return toAjax(heartElderlyInfoService.deleteHeartElderlyInfoByIds(elderlyids));
    }

}
